package com.controller;

import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.domain.Pomr;
import com.domain.UserBls;
import com.domain.UserCj;
import com.domain.UserCs;
import com.domain.UserRllx;
import com.domain.UserYs;
import com.domain.UserYwgms;
import com.service.PomrService;
import com.utils.ObjectUtils;
import com.utils.Ret;

@Controller
public class PomrController {
	
	@Autowired
	PomrService pomrService;
	
	@RequestMapping("pomrIndex.do")
	String pomrIndex(@RequestParam(value="jkdah", defaultValue="")String jkdah,
			@RequestParam(value="xm", defaultValue="")String xm,
			@RequestParam(value="zjh", defaultValue="")String zjh,
			@RequestParam(value="brdh", defaultValue="")String brdh,
			@RequestParam(value="pageNum", defaultValue="1")int pageNum,
			@RequestParam(value="pageSize",defaultValue="10")int pageSize, 
			HttpServletRequest request, Model model){
		
		
		String ocode = (String)request.getSession().getAttribute("ocode");
		if(ocode.equals("0")){
			ocode="";
		}
		List<Pomr> pomrs = new ArrayList<Pomr>();
		int count = 0;
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("jkdah", jkdah);
		params.put("xm", xm);
		params.put("zjh", zjh);
		params.put("brdh",brdh );
		params.put("ocode",ocode );
		params.put("pageBegin", (pageNum-1)*pageSize);
		params.put("pageSize", pageSize);
		
		
		pomrs = pomrService.getPomr(params);
		count = pomrService.getCount(params);
		params.put("totalCount", count);
		
		
		model.addAttribute("params",params);
		model.addAttribute("pomrs", pomrs);
		return "pomr/pomr";
	}
	
	@RequestMapping("toUpdatePomr.do")
	public String getPomr(@RequestParam(value="uid") int uid,Model model){
		
		Pomr pomr = pomrService.getPomrByUid(uid);
		Map<String,List<Object>> pomrCascade = pomrService.getPomraCascade(uid);//没有时间的
		Map<String, List<Map<String, Object>>> pomrCascadeSj = pomrService.getPomrCascadeSJ(uid);//有时间的
		model.addAttribute("pomr", pomr);
		model.addAttribute("mapPomrInfo", pomrService.getPomrMapInfo(uid));
		model.addAttribute("pomrCascade",pomrCascade);
		
		//获得全部的类型表数据
		model.addAttribute("dmZjlx", pomrService.getDmZjlx());
		model.addAttribute("dmHyzk", pomrService.getDmHyzk());
		model.addAttribute("dmZw", pomrService.getDmZw());
		model.addAttribute("dmWhcd", pomrService.getDmWhcd());
		model.addAttribute("dmXx", pomrService.getDmXx());
		model.addAttribute("dmRqlx", pomrService.getDmRqlx());
		model.addAttribute("dmJzlx", pomrService.getDmJzlx());
		
		//获得代码表有的，和没有的
		Map<String,List<Object>> tmpMapDm = new HashMap<String,List<Object>>();//各种代码表
		Map<String,List<Object>> mapDm = new HashMap<String,List<Object>>();
		Map<String,List<Map<String,Object>>> mapDmSj = new HashMap<String,List<Map<String,Object>>>();
		tmpMapDm = pomrService.getDm();
		/*数据转换*/
		//药物过敏史
		List<Object> tmpListYwgms = pomrCascade.get("ywgms");
		if(null!=tmpListYwgms){
			for(int i=0;i<tmpListYwgms.size();i++){
				tmpMapDm.get("ywgms").remove(tmpListYwgms.get(i));
			}
		}
		mapDm.put("ywgms_y", tmpListYwgms);
		mapDm.put("ywgms_n", tmpMapDm.get("ywgms"));

		//暴露史
		List<Object> tmpListBls = pomrCascade.get("bls");
		if(null!=tmpListBls){
			for(int i=0;i<tmpListBls.size();i++){
				tmpMapDm.get("bls").remove(tmpListBls.get(i));
			}
		}
		mapDm.put("bls_y", tmpListBls);
		mapDm.put("bls_n", tmpMapDm.get("bls"));		
		
		//残疾
		List<Object> tmpListCj = pomrCascade.get("cj");
		if(null != tmpListCj ){
			for(int i=0;i<tmpListCj.size();i++){
				tmpMapDm.get("cj").remove(tmpListCj.get(i));
			}
		}
		mapDm.put("cj_y", tmpListCj);
		mapDm.put("cj_n", tmpMapDm.get("cj"));
		
		//燃料类型
		List<Object> tmpListRllx = pomrCascade.get("rllx");
		if(null != tmpListRllx ){
			for(int i=0;i<tmpListRllx.size();i++){
				tmpMapDm.get("rllx").remove(tmpListRllx.get(i));
			}
		}
		mapDm.put("rllx_y", tmpListRllx);
		mapDm.put("rllx_n", tmpMapDm.get("rllx"));
				
		//饮水
		List<Object> tmpListYs = pomrCascade.get("ys");
		if(null != tmpListYs){
			for(int i=0;i<tmpListYs.size();i++){
				tmpMapDm.get("ys").remove(tmpListYs.get(i));
			}
		}
		mapDm.put("ys_y", tmpListYs);
		mapDm.put("ys_n", tmpMapDm.get("ys"));
		
		//厕所
		List<Object> tmpListCs = pomrCascade.get("cs");
		if(null != tmpListCs){
			for(int i=0;i<tmpListCs.size();i++){
				tmpMapDm.get("cs").remove(tmpListCs.get(i));
			}
		}
		mapDm.put("cs_y", tmpListCs);
		mapDm.put("cs_n", tmpMapDm.get("cs"));
		
		//疾病
		List<Map<String,Object>> tmpListJb = pomrCascadeSj.get("jb");
		List<Map<String,Object>> tmpMapDmJb = new ArrayList<Map<String,Object>>();
		if(null != tmpListJb){
			for(int i=0;i<tmpListJb.size();i++){
				tmpMapDm.get("jb").remove(tmpListJb.get(i).get("mc"));
			}
		}
		if(null != tmpMapDm){
			//封装为有时间
			for(int i=0;i<tmpMapDm.get("jb").size();i++){
				Map<String,Object> tmpMap = new HashMap<String,Object>();
				tmpMap.put("mc",tmpMapDm.get("jb").get(i));
				tmpMap.put("qzsj","");
				
				tmpMapDmJb.add(tmpMap);
			}
		}
		mapDmSj.put("jb_y", tmpListJb);
		mapDmSj.put("jb_n", tmpMapDmJb);		
		
		//手术
		List<Map<String,Object>> tmpListSs = pomrCascadeSj.get("ss");
		List<Map<String,Object>> tmpMapDmSs = new ArrayList<Map<String,Object>>();
		if(null!=tmpListSs){
			for(int i=0;i<tmpListSs.size();i++){
				tmpMapDm.get("ss").remove(tmpListSs.get(i).get("mc"));
			}
		}
		if(null != tmpMapDm){
			//封装为有时间
			for(int i=0;i<tmpMapDm.get("ss").size();i++){
				Map<String,Object> tmpMap = new HashMap<String,Object>();
				tmpMap.put("mc",tmpMapDm.get("ss").get(i));
				tmpMap.put("sssj","");
				tmpMapDmSs.add(tmpMap);
			}
		}
		mapDmSj.put("ss_y", tmpListSs);
		mapDmSj.put("ss_n", tmpMapDmSs);		
		
		//外伤
		List<Map<String,Object>> tmpListWs = pomrCascadeSj.get("ws");
		List<Map<String,Object>> tmpMapDmWs = new ArrayList<Map<String,Object>>();
		if(null!=tmpListWs){
			for(int i=0;i<tmpListWs.size();i++){
				tmpMapDm.get("ws").remove(tmpListWs.get(i).get("mc"));
			}
		}
		if(null!=tmpMapDm){
			//封装为有时间
			for(int i=0;i<tmpMapDm.get("ws").size();i++){
				Map<String,Object> tmpMap = new HashMap<String,Object>();
				tmpMap.put("mc",tmpMapDm.get("ws").get(i));
				tmpMap.put("qzsj","");
				tmpMapDmWs.add(tmpMap);
			}
		}
		mapDmSj.put("ws_y", tmpListWs);
		mapDmSj.put("ws_n", tmpMapDmWs);	
		
		model.addAttribute("pomrCascadeSj",mapDmSj);
		
		//输血
		List<Map<String,Object>> tmpListSx = pomrCascadeSj.get("sx");
		List<Map<String,Object>> tmpMapDmSx = new ArrayList<Map<String,Object>>();
		if(null != tmpListSx){
			for(int i=0;i<tmpListSx.size();i++){
				tmpMapDm.get("sx").remove(tmpListSx.get(i).get("mc"));
			}
		}
		if(null!=tmpMapDm){
			//封装为有时间
			for(int i=0;i<tmpMapDm.get("sx").size();i++){
				Map<String,Object> tmpMap = new HashMap<String,Object>();
				tmpMap.put("mc",tmpMapDm.get("sx").get(i));
				tmpMap.put("sxsj","");
				tmpMapDmSx.add(tmpMap);
			}
		}
		mapDmSj.put("sx_y", tmpListSx);
		mapDmSj.put("sx_n", tmpMapDmSx);	
		
		model.addAttribute("mapDm", mapDm);
		model.addAttribute("pomrCascadeSj",mapDmSj);

		
		return "pomr/showPomr";
	}
	
	
	@RequestMapping("doUpdatePomr.do")
	public void doUpdatePomr(HttpServletRequest request, PrintWriter out) {
		Ret ret = new Ret();
		ret.setStatusCode("200");
		ret.setMessage("更新成功！");
		
		Pomr pomr = new Pomr();
		//获得数据封装数据
		pomr.setJkdah(ObjectUtils.getIntVal(request.getParameter("jkdah")));
		pomr.setJdjg(ObjectUtils.getIntVal(request.getParameter("jdjg")));
		pomr.setJdr(ObjectUtils.getIntVal(request.getParameter("jdr")));
		pomr.setXm(ObjectUtils.getStrVal(request.getParameter("xm")));
		pomr.setXbm(ObjectUtils.getIntVal(request.getParameter("xbm")));
		try {
			pomr.setJdsj(ObjectUtils.getDateVal(request.getParameter("jdsj")));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		pomr.setUid(ObjectUtils.getIntVal(request.getParameter("uid")));
		pomr.setZjlx(ObjectUtils.getIntVal(request.getParameter("zjlx")));
		pomr.setZjh(ObjectUtils.getStrVal(request.getParameter("zjh")));
		pomr.setJdah(ObjectUtils.getIntVal(request.getParameter("jdah")));
		pomr.setHyzk(ObjectUtils.getIntVal(request.getParameter("hyzk")));
		pomr.setZw(ObjectUtils.getIntVal(request.getParameter("zw")));
		pomr.setWhcd(ObjectUtils.getIntVal(request.getParameter("whcd")));
		pomr.setBrdh(ObjectUtils.getStrVal(request.getParameter("brdh")));
		pomr.setLxrdh(ObjectUtils.getStrVal(request.getParameter("lxrdh")));
		pomr.setLxrxm(ObjectUtils.getStrVal(request.getParameter("lxrxm")));
		pomr.setFqxm(ObjectUtils.getStrVal(request.getParameter("fqxm")));
		pomr.setMqxm(ObjectUtils.getStrVal(request.getParameter("mqxm")));
		pomr.setJhrxm(ObjectUtils.getStrVal(request.getParameter("jhrxm")));
		pomr.setJtdh(ObjectUtils.getStrVal(request.getParameter("jtdh")));
		pomr.setRqlx(ObjectUtils.getIntVal(request.getParameter("rqlx")));
		pomr.setXzzqwh(ObjectUtils.getStrVal(request.getParameter("xzzqwh")));
		pomr.setHjdwyh(ObjectUtils.getStrVal(request.getParameter("hjdwyh")));
		pomr.setXzz(ObjectUtils.getStrVal(request.getParameter("xzz")));
		pomr.setHjzz(ObjectUtils.getStrVal(request.getParameter("hjzz")));
		pomr.setRh(ObjectUtils.getStrVal(request.getParameter("rh")));

		try {
			int uid = ObjectUtils.getIntVal(request.getParameter("uid"));
			pomrService.updatePomr(pomr);
			
			//对药物过敏
			String[] ywgms = request.getParameterValues("ywgms");
			pomrService.deleteUserYwgms(uid);
			for(int i=0;i<ywgms.length;i++){
				UserYwgms userywgms	= new UserYwgms();
				userywgms.setUid(uid);
				userywgms.setYid(pomrService.getYwgmsIdByMc(ywgms[i]));
				pomrService.addUserYwgms(userywgms);	
			}
			//暴露史
			String[] bls = request.getParameterValues("bls");
			pomrService.deleteUserBls(uid);
			for(int i=0;i<bls.length;i++){
				UserBls userybls = new UserBls();
				userybls.setUid(uid);
				userybls.setBid(pomrService.getBlsIdByMc(bls[i]));
				pomrService.addUserBls(userybls);	
			}
			//残疾
			String[] cj = request.getParameterValues("cj");
			pomrService.deleteUserCj(uid);
			for(int i=0;i<cj.length;i++){
				UserCj userycj = new UserCj();
				userycj.setUid(uid);
				userycj.setCid(pomrService.getCjIdByMc(cj[i]));
				pomrService.addUserCj(userycj);	
			}
			//燃料类型
			String[] rllx  = request.getParameterValues("rllx");
			pomrService.deleteUserRllx(uid);
			for(int i=0;i<rllx.length;i++){
				UserRllx userrllx = new UserRllx();
				userrllx.setUid(uid);
				userrllx.setRid(pomrService.getRllxIdByMc(rllx[i]));
				pomrService.addUserRllx(userrllx);	
			}
			//厕所类型
			String[] cs  = request.getParameterValues("cs");
			pomrService.deleteUserCs(uid);
			for(int i=0;i<cs.length;i++){
				UserCs usercs = new UserCs();
				usercs.setUid(uid);
				usercs.setCid(pomrService.getCsIdByMc(cs[i]));
				pomrService.addUserCs(usercs);	
			}
			//饮水所类型
			String[] ys  = request.getParameterValues("ys");
			pomrService.deleteUserYs(uid);
			for(int i=0;i<ys.length;i++){
				UserYs userys = new UserYs();
				userys.setUid(uid);
				userys.setYid(pomrService.getYsIdByMc(ys[i]));
				pomrService.addUserYs(userys);	
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			ret.setStatusCode("500");
			ret.setMessage("更新失败！");
		}
		
		out.print(ret.toJsonString());
		out.flush();
		out.close();
	}
	
	@RequestMapping("getPomrByJkdah.do")
	public void getPomrByUid(@RequestParam("uid") int uid,PrintWriter out){
		System.out.println(uid);
	}
	
	@RequestMapping("toAddPomr.do")
	public String toAddPomr(Model model,HttpServletRequest request){
		
		int ocode = Integer.parseInt(request.getSession().getAttribute("ocode").toString());
		model.addAttribute("ocode", ocode);
		
		int newJkdah = pomrService.getNewJkdah(ocode);
		model.addAttribute("newJkdah", newJkdah);
		
		model.addAttribute("jdr", request.getSession().getAttribute("ucode"));
		
		
		//获得全部的类型表数据
		model.addAttribute("dmZjlx", pomrService.getDmZjlx());
		model.addAttribute("dmHyzk", pomrService.getDmHyzk());
		model.addAttribute("dmZw", pomrService.getDmZw());
		model.addAttribute("dmWhcd", pomrService.getDmWhcd());
		model.addAttribute("dmXx", pomrService.getDmXx());
		model.addAttribute("dmRqlx", pomrService.getDmRqlx());
		model.addAttribute("dmJzlx", pomrService.getDmJzlx());
		
		model.addAttribute("ywgms", pomrService.getDmYwgms());
		model.addAttribute("bls", pomrService.getDmBls());
		model.addAttribute("cj", pomrService.getDmCj());
		model.addAttribute("rllx", pomrService.getDmRllx());
		model.addAttribute("cs", pomrService.getDmCs());
		model.addAttribute("ys", pomrService.getDmYs());
		model.addAttribute("jb", pomrService.getDmJb());
		model.addAttribute("ss", pomrService.getDmSs());
		model.addAttribute("ws", pomrService.getDmWs());
		model.addAttribute("sx", pomrService.getDmSx());
		
		return "pomr/toaddpomr";
	}
	
	@RequestMapping("doAddPomr.do")
	public void toAddPomr(Model model,HttpServletRequest request,PrintWriter out,Ret ret){
		
		ret.setStatusCode("200");
		ret.setMessage("添加成功！");
		
		Pomr pomr = new Pomr();
		int jkdah = ObjectUtils.getIntVal(request.getParameter("jkdah"));
		//获得数据封装数据
		pomr.setJkdah(jkdah );
		pomr.setJdjg(ObjectUtils.getIntVal(request.getParameter("jdjg")));
		
		pomr.setJdr(1);
		pomr.setXm(ObjectUtils.getStrVal(request.getParameter("xm")));
		pomr.setXbm(ObjectUtils.getIntVal(request.getParameter("xbm")));
		
		try {
			if(!request.getParameter("jdsj").equals("")){
				pomr.setJdsj(ObjectUtils.getDateVal(request.getParameter("jdsj")));
			}
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		pomr.setUid(ObjectUtils.getIntVal(request.getParameter("uid")));
		pomr.setZjlx(ObjectUtils.getIntVal(request.getParameter("zjlx")));
		pomr.setZjh(ObjectUtils.getStrVal(request.getParameter("zjh")));
		pomr.setJdah(ObjectUtils.getIntVal(request.getParameter("jdah")));
		pomr.setHyzk(ObjectUtils.getIntVal(request.getParameter("hyzk")));
		pomr.setZw(ObjectUtils.getIntVal(request.getParameter("zw")));
		pomr.setWhcd(ObjectUtils.getIntVal(request.getParameter("whcd")));
		pomr.setBrdh(ObjectUtils.getStrVal(request.getParameter("brdh")));
		pomr.setLxrdh(ObjectUtils.getStrVal(request.getParameter("lxrdh")));
		pomr.setLxrxm(ObjectUtils.getStrVal(request.getParameter("lxrxm")));
		pomr.setFqxm(ObjectUtils.getStrVal(request.getParameter("fqxm")));
		pomr.setMqxm(ObjectUtils.getStrVal(request.getParameter("mqxm")));
		pomr.setJhrxm(ObjectUtils.getStrVal(request.getParameter("jhrxm")));
		pomr.setJtdh(ObjectUtils.getStrVal(request.getParameter("jtdh")));
		pomr.setRqlx(ObjectUtils.getIntVal(request.getParameter("rqlx")));
		pomr.setXzzqwh(ObjectUtils.getStrVal(request.getParameter("xzzqwh")));
		pomr.setHjdwyh(ObjectUtils.getStrVal(request.getParameter("hjdwyh")));
		pomr.setXzz(ObjectUtils.getStrVal(request.getParameter("xzz")));
		pomr.setHjzz(ObjectUtils.getStrVal(request.getParameter("hjzz")));
		pomr.setRh(ObjectUtils.getStrVal(request.getParameter("rh")));

		try {
			pomrService.addPomr(pomr);
			int uid1 = pomrService.getUidByJkdah(jkdah);
			
			//对药物过敏
			String[] ywgms = request.getParameterValues("ywgms");
			if(null != ywgms){
				for(int i=0;i<ywgms.length;i++){
					UserYwgms userywgms	= new UserYwgms();
					userywgms.setUid(uid1);
					userywgms.setYid(ObjectUtils.getIntVal(ywgms[i].toString()));
					pomrService.addUserYwgms(userywgms);	
				}
			}
			
			//暴露史
			String[] bls = request.getParameterValues("bls");
			if(bls != null){
				for(int i=0;i<bls.length;i++){
					UserBls userybls = new UserBls();
					userybls.setUid(uid1);
					userybls.setBid(ObjectUtils.getIntVal(bls[i].toString()));
					pomrService.addUserBls(userybls);	
				}
			}
			
			//残疾
			String[] cj = request.getParameterValues("cj");
			if(null != cj){
				for(int i=0;i<cj.length;i++){
					UserCj userycj = new UserCj();
					userycj.setUid(uid1);
					userycj.setCid(ObjectUtils.getIntVal(cj[i].toString()));
					pomrService.addUserCj(userycj);	
				}
			}
			
			//燃料类型
			String[] rllx  = request.getParameterValues("rllx");
			if(null != rllx){
				for(int i=0;i<rllx.length;i++){
					UserRllx userrllx = new UserRllx();
					userrllx.setUid(uid1);
					userrllx.setRid(ObjectUtils.getIntVal(rllx[i].toString()));
					pomrService.addUserRllx(userrllx);	
				}
			}
			
			//厕所类型
			String[] cs  = request.getParameterValues("cs");
			if(null != cs){
				for(int i=0;i<cs.length;i++){
					UserCs usercs = new UserCs();
					usercs.setUid(uid1);
					usercs.setCid(ObjectUtils.getIntVal(cs[i].toString()));
					pomrService.addUserCs(usercs);	
				}
			}
			
			//饮水所类型
			String[] ys  = request.getParameterValues("ys");
			if(null != ys){
				for(int i=0;i<ys.length;i++){
					UserYs userys = new UserYs();
					userys.setUid(uid1);
					userys.setYid(ObjectUtils.getIntVal(ys[i].toString()));
					pomrService.addUserYs(userys);	
				}
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			ret.setStatusCode("500");
			ret.setMessage("添加失败！");
		}
		
		out.print(ret.toJsonString());
		out.flush();
		out.close();
	}
	
}
